package ru.uskov.trainer.datastore.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import ru.uskov.trainer.datastore.IUser;
import ru.uskov.trainer.datastore.PMF;
import ru.uskov.trainer.model.IProductItem;
import ru.uskov.trainer.model.impl.ProductItem;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User implements IUser {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.UNSPECIFIED)
  public String id;
  
  @Persistent
  private String weeight;
  
  @Persistent
  private String wantedWeight;
  
  @Persistent
  private String height;
  
  User(String id) {
    this.id = id;
  }

  @Override
  public String getId() {
    return id;
  }

  @Override
  public String getWeight() {
    return weeight;
  }

  @Override
  public String getWantedWeight() {
    return wantedWeight;
  }

  @Override
  public String getHeight() {
    return height;
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<IProductItem> getProducts() {
    long start = System.currentTimeMillis();
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
      String query = "select from " + ProductItem.class.getName() + " where userId==\"" + this.id + "\" order by addDate asc";
      List<ProductItem> products = (List<ProductItem>) pm.newQuery(query).execute(); 
      List<IProductItem> result =  new ArrayList<IProductItem>();
      for (ProductItem product: products) {
        result.add(new ProductItem(product));
      }
      return result;
    } finally {
      pm.close();
      System.out.println("***Getting products took: " + (System.currentTimeMillis() - start));
    }
  }

  @Override
  public void addProduct(IProductItem item) {
    long start = System.currentTimeMillis();    
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
      pm.makePersistent(item);
    } finally {
      pm.close();
    }
    System.out.println("***Adding product took: " + (System.currentTimeMillis() - start));
  }

  @Override
  public void setWidth(String weight) {
    this.weeight = weight;
  }

  @Override
  public void setHeight(String height) {
    this.height = height;
  }

  @Override
  public void setWantedWeigth(String wantedWeight) {
    this.wantedWeight = wantedWeight;
  }

  @Override
  public void removeProduct(int productIndex) {
    long start = System.currentTimeMillis();
    List<ProductItem> products = (List<ProductItem>)((Object)getProducts());
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
      ProductItem product = products.get(productIndex);
        pm.deletePersistent( pm.getObjectById( product.getClass(),
            product.getId() ) ); 
      
    } finally {
      pm.close();
      System.out.println("***Removing product took: " + (System.currentTimeMillis() - start));
    }
  }

  @Override
  public void removeAll() {
    // TODO 463 ms
      long start = System.currentTimeMillis();
      List<ProductItem> products = (List<ProductItem>)((Object)getProducts());
      PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
      for (ProductItem product : products) {
        //pm.deletePersistent(product);
        pm.deletePersistent( pm.getObjectById( product.getClass(),
            product.getId() ) ); 
      }
    } finally {
      pm.close();
      System.out.println("***Removing products took: " + (System.currentTimeMillis() - start));
    }
  }
}
